﻿using System.Collections;
using System.Collections.Generic;
using IDAL;
using Model;

namespace BLL
{
    public class KeywordService
    {
        private static readonly IKeywordDAL dal = DALFactory.DataAccess.createKeywordDAL();

        //分割关键字
        public IList<string> breakUpKeyword(string keywords)
        {
            string[] keywordArray = keywords.Split(new char[1] { ',' });
            IList<string> keywordNameList = new List<string>();
            foreach (string keywordName in keywordArray)
            {
                if (!keywordNameList.Contains(keywordName))
                {
                    keywordNameList.Add(keywordName);
                }
            }
            return keywordNameList;
        }

        //保存关键字
        public Keyword saveKeyword(string keywordName)
        {
            Keyword keyword = getKeyword(keywordName);
            if (keyword != null)
            {
                return keyword;
            }
            keyword = new Keyword()
            {
                keywordName = keywordName
            };
            dal.saveKeyword(keyword);
            return keyword;
        }

        //保存关键字
        public void saveKeyword(string keywords, int resourceId)
        {
            IList<string> keywordNameList = breakUpKeyword(keywords);
            IList<Keyword> keywordList = new List<Keyword>();
            ResourceService rs = new ResourceService();
            Keyword keyword;
            foreach (string keyworName in keywordNameList)
            {
                keyword = new Keyword()
                {
                    keywordName = keyworName,
                    resource = rs.getResourceById(resourceId)
                };
                keywordList.Add(keyword);
            }
            dal.saveKeyword(keywordList);
        }

        //删除关键字
        public Hashtable delKeywordNums(int id)
        {
            Hashtable ht = new Hashtable();
            Keyword keyword = getKeyword(id);
            if (keyword == null)
            {

                ht.Add("result", "0");
                ht.Add("message", "关键字不存在");
                return ht;
            }
            dal.delkeyword(keyword);
            ht.Add("result", "1");
            ht.Add("message", "删除成功");
            return ht;
        }

        //删除关键字
        public Hashtable delKeywords(int resourceId)
        {
            Hashtable ht = new Hashtable();
            Resource resource = new ResourceService().getResourceById(resourceId);
            if (resource == null)
            {

                ht.Add("result", "0");
                ht.Add("message", "资源不存在");
                return ht;
            }
            dal.delkeyword(resource);
            ht.Add("result", "1");
            ht.Add("message", "删除成功");
            return ht;
        }

        //得到关键字列表
        public IList<Keyword> getKeywords()
        {
            return dal.getKeywordList();
        }

        //根据ID得到关键字
        public Keyword getKeyword(int id)
        {
            return dal.getKeyword(id);
        }

        //根据name得到关键字
        public Keyword getKeyword(string name)
        {
            return dal.getKeyword(name);
        }

        //根据resource得到关键字
        public IList<Keyword> getKeywords(Resource resource)
        {
            return dal.getKeywordList(resource);
        }

        

    }
}
